<?php
/**
 * InterfaceController
 *
 * PHP Version 5.4
 *
 * @author liuxuTJ <liuxuphp@icesmart.cn>
 */
namespace Admin\Controller;
use Admin\Service\PermissionService;
use Admin\Service\UserService;
use Model\GiftCostLogsModel;
use Model\GiftModel;
use Model\GiftProportionModel;
use Model\InterfaceModel;
use Model\RoomModel;
use Model\UserModel;
use Think\Controller;

/**
 * Class InterfaceController
 */
class InterfaceController extends BaseController {

    public function __construct()
    {
        parent::__construct();
        $userService = new UserService();
        $userInfo = $userService->validateLogin();   //验证是否登录

        $permissionService = new PermissionService();
        $permission = $permissionService->validate($userInfo['userId'], [
            'add' => '/'.CONTROLLER_NAME.'/add',
            'del' => '/'.CONTROLLER_NAME.'/del',
            'edit' => '/'.CONTROLLER_NAME.'/edit'
        ]);

        $menu = $permissionService->menu($userInfo['userId']);

        $this->assign('menu', $menu);
        $this->assign('permission', $permission);
        $this->assign('userInfo', $userInfo);
    }


    /**
     * 接口列表 查询
     */
    public function index()
    {
        $interfaceModel = new InterfaceModel();
        $page = $this->page($interfaceModel, null, 15);
        $show = $page->show();
        $interfaceList = $interfaceModel->order('createTime DESC')->limit($page->firstRow.','.$page->listRows)->select();

        $this->assign('url', '/Interface/index');
        $this->assign('interfaceList', $interfaceList);
        $this->assign('page',$show);
        $this->display();
    }

    /**
     * 添加接口
     */
    public function add()
    {
        $name = I('post.name');
        $url = I('post.url');
        $createTime = date('Y-m-d H:i:s');

        if(empty($name) || empty($url)){
            $this->error('带*处为必填项');
        }

        $interfaceModel = new InterfaceModel();
        $interfaceAdd = $interfaceModel->add([
            'name' => $name,
            'url' => $url,
            'createTime' => $createTime
        ]);

        if($interfaceAdd){
            $this->success('添加成功', '/Admin/Interface', 1);
        }else{
            $this->error('添加失败');
        }
    }

    /**
     * 编辑接口
     */
    public function edit()
    {
        $interfaceId = I('get.id');
        $interfaceModel = new InterfaceModel();
        $interfaceInfo = $interfaceModel->get($interfaceId);

        $this->assign('url', '/Interface/index');
        $this->assign('interfaceInfo', $interfaceInfo);
        $this->display();
    }


    /**
     * 保存接口
     */
    public function save()
    {
        $interfaceId = I('post.id');
        $name = I('post.name');
        $url = I('post.url');
        $status = I('post.status');

        if(empty($name) || empty($url)){
            $this->error('带*处为必填项');
        }

        $data['name'] = $name;
        $data['url'] = $url;
        $data['status'] = $status;

        $interfaceModel = new InterfaceModel();
        $interfaceEdit = $interfaceModel->where('id = '.$interfaceId)->save($data);

        if($interfaceEdit){
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }

    /**
     * 删除接口
     */
    public function del()
    {
        $interfaceId = I('get.id');

        $interfaceModel = new InterfaceModel();
        $interfaceDel = $interfaceModel->where('id = '.$interfaceId)->delete();

        if($interfaceDel){
            $this->success('删除成功', '/Admin/Interface', 1);
        }else{
            $this->error('删除失败');
        }
    }
}